# -*- coding: UTF-8 -*-
import os
from config.default_settings import mysql_database_003
from pathlib import Path
from loguru import logger

from utils.datetime_util import DateTimeHelper

# 模版
template = "sqlacodegen  --options noconstraints --noviews --outfile {file_path} {connect_url}"


def generator_template_code(file_path:str,connect_url:str,tables:list):
    args = template.format(file_path=file_path, connect_url=connect_url)
    if tables is not None and len(tables) > 0:
        args = args + " --tables " + ",".join(tables)
    logger.info("\n"+ args)
    os.system(args)
    logger.info('模板文件生成完成,路径:{}',file_path)

if __name__ == '__main__':
    date_time_helper=DateTimeHelper()
    formatted= "../output/"+date_time_helper.format_now("%y%m%d") + ".py"

    # 当前脚本所在目录
    script_dir = Path(__file__).parent.resolve()
    file_path = os.path.join(script_dir, formatted)
    tables = ["bgt_account_sql","bas_mof_div"]
    generator_template_code(file_path,mysql_database_003.get_connect_url(),tables)

